﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using SaleManagement.Web.App_Code;
using SaleManagement.Services;
using SaleManagement.Domains;

namespace SaleManagement.Web.Pages
{
  public partial class Login : BasePage
  {
    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack && !IsCallback)
      {
        txtUserName.Focus();
      }
    }

    protected void btnSignIn_Click(object sender, EventArgs e)
    {
      if (!string.IsNullOrEmpty(txtUserName.Text) && !string.IsNullOrEmpty(txtPassword.Text))
      {
        User user = new UserService().GetByUsername(Utilities.SafeString(txtUserName.Text));
        if (user != null && user.Password == Library.Encryption.MD5.Encrypt(txtPassword.Text))
        {
          if (user.Status == StatusList.UserStatus.LOCKED)
          {
            lblError.Text = "Tài khoản này đã bị khóa";
            return;
          }

          CurrentUser = user;
          int total = -1;
          //Get user's permission here
          CurrentUserFunctions = new UserFunctionService().GetPaged(string.Format("[UserName] = '{0}'", user.Username), "", 0, int.MaxValue, out total);
          Response.Redirect("~/Pages/ChangeUserInfo.aspx", false);
          return;
        }
      }

      lblError.Text = "Tên đăng nhập hoặc mật khẩu không hợp lệ";
    }
    #endregion
  }
}
